package com.heeexy.example.service;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class SendSms {
    public boolean send(String phoneNum, String temlateCode, Map<String,Object> code){
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI5tFfsetyqcvjhomUWc9m", "秘钥值");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

        request.putQueryParameter("RegionId", "cn-hangzhou");

        request.putQueryParameter("PhoneNumbers", phoneNum);//手机号
        //       request.putQueryParameter("SignName", "签名名称");
        request.putQueryParameter("TemplateCode", temlateCode);
//        request.putQueryParameter("TemplateParam", "填充的参数（JSON对象格式）");
        request.putQueryParameter("TemplateCode", JSONObject.toJSONString(code));
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getHttpResponse().isSuccess();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }
}
